from rest_framework import serializers
from goods.models import SKU, GoodsCategory, SPU, SPUSpecification, SpecificationOption


class SkuViewSetSerializer(serializers.ModelSerializer):
    # 重写分类
    category = serializers.StringRelatedField(read_only=True)
    category_id = serializers.IntegerField()

    # 重写spu
    spu = serializers.StringRelatedField(read_only=True)
    spu_id = serializers.IntegerField()

    class Meta:
        model = SKU
        fields = "__all__"


# 定义获取商品分类序列化器
class SkuCategoriesModelSerializers(serializers.ModelSerializer):

    class Meta:
        model = GoodsCategory
        fields = ("id", "name")


# 定义获取spu序列化器
class GoodSimpleModelSerializers(serializers.ModelSerializer):
    class Meta:
        model = SPU
        fields = ("id", "name")


# 规格选项序列化器
class SpecificationOptionModelSerializers(serializers.ModelSerializer):
    class Meta:
        model = SpecificationOption
        fields = ("id", "value")


# 定义获取spu_option序列化器
class GoodsIdSpecModelSerializers(serializers.ModelSerializer):
    # 调用序列化器返回规格选项
    options = SpecificationOptionModelSerializers(read_only=True, many=True)

    class Meta:
        model = SPUSpecification
        fields = "__all__"
